<?php
  

    function osc_runAlert($type = null) {

        if ( !in_array($type, array('HOURLY', 'DAILY', 'WEEKLY', 'INSTANT')) ) {
            return;
        }

        $internal_name = 'alert_email_hourly' ;
        switch ($type) {
            case 'HOURLY':
                $internal_name = 'alert_email_hourly' ;
            break;
            case 'DAILY':
                $internal_name = 'alert_email_daily' ;
            break;
            case 'WEEKLY':
                $internal_name = 'alert_email_weekly' ;
            break;
            case 'INSTANT':
                $internal_name = 'alert_email_instant' ;
            break;
        }

        $active = TRUE;
        $searches = Alerts::newInstance()->getAlertsByTypeGroup($type,$active) ;
        foreach ($searches as $s_search) {
            $a_search = Search::newInstance();

            // Get if there're new ads on this search
            $a_search = osc_unserialize(base64_decode($s_search['s_search'])) ;
            $cron = Cron::newInstance()->getCronByType($type);
            if (is_array($cron)) {
                $last_exec = $cron['d_last_exec'] ;
            } else {
                $last_exec = '0000-00-00 00:00:00' ;
            }

            $a_search->addConditions(sprintf(" %st_item.dt_pub_date > '%s' ", DB_TABLE_PREFIX, $last_exec)) ;

            $totalItems = $a_search->count();
            $items = $a_search->doSearch();

            if (count($items) > 0) {
                //If we have new items from last check
                //Catch the user subscribed to this search
                $users = Alerts::newInstance()->getUsersBySearchAndType($s_search['s_search'], $type, $active) ;

                if (count($users) > 0 ) {

                    $prefLocale = osc_language() ;
                    $page = Page::newInstance()->findByInternalName($internal_name) ;

                    $page_description = $page['locale'] ;

                    $_title = $page_description[$prefLocale]['s_title'] ;
                    $_body  = $page_description[$prefLocale]['s_text'] ;

                    $ads = "";
                    foreach ($items as $item) {
                        $ads .= '<a href="'. osc_item_url_ns($item['pk_i_id']).'">' . $item['s_title'] . '</a><br/>' ;
                    }

                    foreach ($users as $user)
                    {
                        if($user['fk_i_user_id']!=0) {
                            $user = User::newInstance()->findByPrimaryKey($user['fk_i_user_id']);
                        } else {
                            $user['s_name'] = $user['s_email'];
                        }

                        $unsub_link = osc_user_unsubscribe_alert_url($user['s_email'], $s_search['s_secret']);

                        $unsub_link = "<a href='". $unsub_link ."'>unsubscribe alert</a>";

                        $words = array() ;
                        $words[] = array('{USER_NAME}', '{USER_EMAIL}', '{ADS}', '{UNSUB_LINK}') ;
                        $words[] = array($user['s_name'], $user['s_email'], $ads, $unsub_link) ;
                        $title = osc_mailBeauty($_title, $words) ;
                        $body = osc_mailBeauty($_body, $words) ;

                        $params = array(
                            'subject' => $title
                            ,'to' => $user['s_email']
                            ,'to_name' => $user['s_name']
                            ,'body' => $body
                            ,'alt_body' => $body
                        ) ;

                        osc_sendMail($params) ;
                    }
                }
            }
        }
    }

?>